<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/6
 * Time: 18:30
 */

namespace XiaoZhuBundle\Model;


class Category
{
    private $REQUEST;
    private $DOCTRINE;

    public function __construct($REQUEST,$DOCTRINE)
    {
        $this->REQUEST = $REQUEST->getCurrentRequest();
        $this->DOCTRINE = $DOCTRINE;
    }

    public function db()
    {
        return $this->DOCTRINE->getManager()->createQueryBuilder();
    }

    public function rep($entityName)
    {
        return $this->DOCTRINE->getManager()->getRepository($entityName);
    }

    public function makeUpCategory($attrs)
    {
        $strCategory = "AdminsBundle:Category";
        $cid = 0;
        $searchInfo = array();
        $cids = array();
        foreach($attrs as $catId => $values){
            $cid = $catId;
            $data = array();
            foreach ($values as $attrNameId => $attrValueId) {
                $data[] = array(
                    "keyId"=> "$attrNameId",
                    "valId"=> "$attrValueId"
                );
            }
            $searchInfo = array_merge($searchInfo ,$data);
        }
        $cids[] = $cid;
        $rep = $this->rep($strCategory);
        do {
            $cat = $rep->find($cid);
            $cid = $cat->getParent();
            if ($cid != 0 ) {
                $cids[] = $cid;
            }
        } while ($cid != 0);

        $catInfo = array("cat"=>$cids,"attr"=>$searchInfo);
        return $catInfo;
    }

    public function getCategoryFromString($str)
    {
        $arr = json_decode($str,true);
        $parem_cat = $arr["cat"];
        $parem_attr = $arr["attr"];
        $cat = $this->rep("AdminsBundle:Category")->findAll();
        $catAttr = $this->rep("AdminsBundle:CategoryAttribute")->findAll();
        $catAttrVal = $this->rep("AdminsBundle:CategoryAttributeValue")->findAll();
        $cats = array();
        foreach ($parem_cat as $cid) {
            foreach ($cat as $category) {
                if ($category->getId() == $cid) {
                    $cats[$cid] = $category->getName();
                }
            }
        }
        $attrs = array();
        foreach ($parem_attr as $key => $attr) {
            if (!$attr["keyId"]) {
                continue;
            }
            foreach ($catAttr as $categoryAttr) {
                if ($categoryAttr->getId() == $attr["keyId"]) {
                    $attrs[$key]["name"] = array(
                        "id"=>$attr["keyId"],
                        "str"=>$categoryAttr->getAttrName()
                    );
                }
            }
            if (!$attr["valId"]) {
                $attrs[$key]["value"] = array(
                    "id"=>0,
                    "str"=>"-"
                );
            }
            foreach ($catAttrVal as $categoryVal) {
                if ($categoryVal->getId() == $attr["valId"]) {
                    $attrs[$key]["value"] = array(
                        "id"=>$attr["valId"],
                        "str"=>$categoryVal->getAttrVal()
                    );
                }
            }
        }
        return array(
            "cat" => $cats,
            "attr"=> $attrs,
        );
    }

    public function getCategorySons( $pid = 0)//从根目录开始
    {

        $rep = $this->rep("AdminsBundle:Category");
        $sons = $rep->findBy(array("parent" => $pid));
        return $sons ;
    }

    public function getCategoryTree( $pid = 0)//从根目录开始
    {
        $categries = $this->getCategorySons($pid);
        if (is_array($categries)) {
            $newArray = array();
            foreach ($categries as $cat) {
                $newArray[] = array(
                    "id"    => $cat->getId(),
                    "name"    => $cat->getName(),
                );
            }
            foreach ($newArray as &$cat) {
                $cat["sons"] = $this->getCategoryTree($cat["id"]);
                if ($cat["sons"] == false) {
                    unset( $cat["sons"] );
                }
            }
            return $newArray;
        } else {
            return false;
        }
    }
    public function getAttrValues($attrId)
    {
        $rep = $this->rep("AdminsBundle:CategoryAttributeValue");
        $vals = $rep->findBy(array("attrId"=>$attrId));
        $values = array();
        foreach ($vals as $val) {
            $values[] = array(
                "id"        => $val->getId(),
                "name"      => $val->getAttrVal(),
                "parent"    => $val->getAttrId()
            );
        }
        return $values;
    }

    public function getAttrAndValues()
    {
        $rep = $this->rep("AdminsBundle:CategoryAttribute");
        $attrs = $rep->findAll();
        $attributes = array();
        foreach ($attrs as $attr) {
            $attributes[] = array(
                "id"        => $attr->getId(),
                "cid"       => $attr->getCid(),
                "name"      => $attr->getAttrName(),
                "sons"      => $this->getAttrValues($attr->getId())
            );
        }
        return $attributes;
    }
}